﻿#include "wqToolItem.h"
#include <QMessageBox>

wqToolItem::wqToolItem( QGraphicsItem* parent /*= 0*/ )
 : QGraphicsObject(parent)
{
	mbHover = false;

	setFlags(QGraphicsItem::ItemIsSelectable |
		QGraphicsItem::ItemIsFocusable);

	setAcceptHoverEvents(true);
}

QRectF wqToolItem::boundingRect() const
{
	return QRectF(0,0,20,20);
}

void wqToolItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* item, QWidget* wid )
{
	painter->save();

	// draw circle
	if(mbHover)
	{
		painter->setPen(QPen(Qt::yellow,3));
		painter->setBrush(QBrush(GetGreen_linearGradient()));
	}
	else
	{
		painter->setPen(QPen(Qt::blue,3));
		painter->setBrush(QBrush(GetYellow_linearGradient()));
	}

	painter->drawRect(boundingRect());

	painter->setPen(QPen(Qt::red, 1, Qt::DotLine));
	painter->setBrush(Qt::NoBrush);
	painter->drawRect(boundingRect());

	painter->restore();
}

QRadialGradient wqToolItem::GetGreen_linearGradient(int OFFSET_Y)
{
	// 绿色
	QRadialGradient linear( boundingRect().center(), 10 );
	linear.setColorAt(0.3, QColor(103,247,91));
	linear.setColorAt(0.6, QColor(97,237,56));
	linear.setColorAt(0.9, QColor(43,43,43));

	return linear;
}

QRadialGradient wqToolItem::GetYellow_linearGradient(int OFFSET_Y)
{
	// 黄色
	QRadialGradient linear( boundingRect().center(), 10 );
	linear.setColorAt(0.3, QColor(235,245,105));
	linear.setColorAt(0.6, QColor(223,240,71));
	linear.setColorAt(0.9, QColor(54,58,3));

	return linear;
}

void wqToolItem::hoverEnterEvent( QGraphicsSceneHoverEvent * event )
{
	mbHover = true;

	QGraphicsItem::hoverEnterEvent(event);
}

void wqToolItem::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
{
	mbHover = false;

	QGraphicsItem::hoverLeaveEvent(event);
}

void wqToolItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
	QMessageBox::information(NULL, "Hello", "This is a tool demo!");

	QGraphicsItem::mousePressEvent(event);
}
